<?php

require_once '../Config.php';
require_once '../utils/SqlHelper.class.php';
function checkUserValidate() {
	@session_start ();
	if (empty ( $_SESSION ['userId'] )) {
		header ( "Location: /" . project . "/login/login.php?errorno=3" );
	}
}
function getuserid(){
	if (empty ( $_SESSION ['userId'] )) {
		header ( "Location: /" . project . "/login/login.php?errorno=3" );
	}
	return $_SESSION ['userId'];
}
function getCon() {
	$con = mysql_pconnect ( host, username, password );
	if (! $con) {
		die ( 'Could not connect: ' . mysql_error () );
	}
	mysql_select_db ( db );
	return $con;
}

// 传入表单文件域的name属性
function upload($name) {
	if (empty ( $_FILES [$name] ['tmp_name'] )) {
		return;
	}
	if (is_uploaded_file ( $_FILES [$name] ['tmp_name'] )) {
		$uploaded_file = $_FILES [$name] ['tmp_name'];
		$project_name = project;
		$project_path = $_SERVER ['DOCUMENT_ROOT'] . "/" . $project_name . "/";
		$today = getdate ();
		$year = $today ['year'];
		$month = $today ['mon'];
		$day = $today ['mday'];
		$move_to_dir = $project_path . $year . "/" . $month . "/" . $day . "/";
		
		if (! file_exists ( $move_to_dir )) {
			mkdir ( $move_to_dir, 0777, true );
		}
		// 防止文件重名
		$file_true_name = $_FILES [$name] ['name'];
		$file_name = time () . rand ( 1, 1000 ) . substr ( $file_true_name, strrpos ( $file_true_name, "." ) );
		$move_to_file = $move_to_dir . $file_name;
		$relative_path = "/" . $year . "/" . $month . "/" . $day . "/" . $file_name;
		if (move_uploaded_file ( $uploaded_file, iconv ( "utf-8", "gb2312", $move_to_file ) )) {
			return $relative_path;
		} else {
			return "";
		}
	} else {
		return "";
	}
}
// 传入表单文件域的name属性
function upload1($name) {
	if (empty ( $_FILES [$name] ['tmp_name'] )) {
		return;
	}
	if (is_uploaded_file ( $_FILES [$name] ['tmp_name'] )) {
		$uploaded_file = $_FILES [$name] ['tmp_name'];
		$project_name = project;
		$project_path = $_SERVER ['DOCUMENT_ROOT'] . "/" . $project_name . "/" . "upload/";
		$today = getdate ();
		$year = $today ['year'];
		$month = $today ['mon'];
		$day = $today ['mday'];
		$move_to_dir = $project_path . $year . "/" . $month . "/" . $day . "/";

		if (! file_exists ( $move_to_dir )) {
			mkdir ( $move_to_dir, 0777, true );
		}
		// 防止文件重名
		$file_true_name = $_FILES [$name] ['name'];
		$file_name = time () . rand ( 1, 1000 ) . substr ( $file_true_name, strrpos ( $file_true_name, "." ) );
		$move_to_file = $move_to_dir . $file_name;
		$relative_path = "/" . $year . "/" . $month . "/" . $day . "/" . $file_name;
		if (move_uploaded_file ( $uploaded_file, iconv ( "utf-8", "gb2312", $move_to_file ) )) {
			return $relative_path;
		} else {
			return "";
		}
	} else {
		return "";
	}
}
// 传入表单文件域的name属性
function uploadAPK($name) {
	if (empty ( $_FILES [$name] ['tmp_name'] )) {
		return;
	}
	if (is_uploaded_file ( $_FILES [$name] ['tmp_name'] )) {
		$uploaded_file = $_FILES [$name] ['tmp_name'];
		$project_name = project;
		$project_path = $_SERVER ['DOCUMENT_ROOT'] . "/" . $project_name . "/";
		$move_to_dir = $project_path . "apk". "/";

		if (! file_exists ( $move_to_dir )) {
			mkdir ( $move_to_dir, 0777, true );
		}
		// 防止文件重名
		$file_true_name = $_FILES [$name] ['name'];
		$file_name = time () . rand ( 1, 1000 ) . substr ( $file_true_name, strrpos ( $file_true_name, "." ) );
		$move_to_file = $move_to_dir . "shangqiu.apk";
		if(file_exists($move_to_file)){
			unlink($move_to_file);
		}
		if (move_uploaded_file ( $uploaded_file, iconv ( "utf-8", "gb2312", $move_to_file ) )) {
			return "/apk". "/"."shangqiu.apk";
		} else {
			return "";
		}
	} else {
		return "";
	}
}

/**
 * 上传多张图片
 */
function uploadMore() {
	if (empty ( $_FILES ["pictures"] ["tmp_name"] [0] )) {
		return "";
	}
	if(empty($_FILES ["pictures"] ["tmp_name"][0])){
		return;
	}
	$dest_folder = "picture/"; // 上传图片保存的路径 图片放在跟你upload.php同级的picture文件夹里
	$arr = array (); // 定义一个数组存放上传图片的名称方便你以后会用的，如果不用那就不写
	$count = 0;
	if (! file_exists ( $dest_folder )) {
		mkdir ( $dest_folder );
	}
	$imgs = "";
	foreach ( $_FILES ["pictures"] ["error"] as $key => $error ) {
		$project_name = project;
		$project_path = $_SERVER['DOCUMENT_ROOT']."/".$project_name."/";
		$today = getdate();
		$year = $today['year'];
		$month = $today['mon'];
		$day = $today['mday'];
		$move_to_dir = $project_path.$year."/".$month."/".$day."/";
		
		
		if (!file_exists($move_to_dir)) {
			mkdir($move_to_dir, 0777, true);
		}
		if ($error == UPLOAD_ERR_OK) {
			$tmp_name = $_FILES ["pictures"] ["tmp_name"] [$key];
			$name1 = $_FILES ["pictures"] ["name"] [$key];
			
			//防止文件重名
			$file_name = time().rand(1, 1000).substr($name1, strrpos($name1, "."));
			$move_to_file = $move_to_dir.$file_name;
			$relative_path = "/".$year."/".$month."/".$day."/".$file_name;
			
			$name = $_FILES ["pictures"] ["name"] [$key];
			$uploadfile = $dest_folder . $name;
			move_uploaded_file ( $tmp_name, $move_to_file );
			$arr [$count] = $relative_path;
			$count ++;
			$relative_path = ip.project.$relative_path;
			if($count == 1){
				$imgs = $imgs.$relative_path;
			}else{
				$imgs = $imgs.";".$relative_path;
			}
		}
	}
	return $imgs;
}
function delFile($path) {
	$file = $_SERVER ['DOCUMENT_ROOT'] . "/" . project . $path;
	if (file_exists ( $file )) {
		unlink ( $file );
	}
}
function getTypeList($parent) {
	$sqlHelper = new SqlHelper ();
	if ($parent) {
		$sql = "select * from tab_type where fatherId = $parent";
	} else {
		$sql = "select * from tab_type where fatherId= -1";
	}
	$res = $sqlHelper->query_row ( $sql );
	$sqlHelper->close_connect ();
	return $res;
}
function getAreaList($parent) {
	$sqlHelper = new SqlHelper ();
	if ($parent) {
		$sql = "select * from tab_area where fatherId = $parent";
	} else {
		$sql = "select * from tab_area where fatherId= -1";
	}
	$res = $sqlHelper->query_row ( $sql );
	$sqlHelper->close_connect ();
	return $res;
}

function getAreaStr($area){
	$sqlHelper = new SqlHelper ();
	$sql = "select * from tab_area where id=".$area;
	$res = $sqlHelper->query_row ( $sql );
	$sqlHelper->close_connect ();
	return $res;
}

?>